package com.housekeeping.service.impl;

import com.housekeeping.dao.OrderDAO;
import com.housekeeping.dao.ReviewDAO;
import com.housekeeping.dao.impl.OrderDAOImpl;
import com.housekeeping.dao.impl.ReviewDAOImpl;
import com.housekeeping.model.Order;
import com.housekeeping.model.Review;
import com.housekeeping.service.ReviewService;

import java.util.Date;
import java.util.List;

public class ReviewServiceImpl implements ReviewService {
    
    private ReviewDAO reviewDAO = new ReviewDAOImpl();
    private OrderDAO orderDAO = new OrderDAOImpl();

    public Review getReviewById(Integer reviewId) {
        return reviewDAO.findById(reviewId);
    }

    public List<Review> getAllReviews() {
        return reviewDAO.findAll();
    }

    public Review getReviewByOrderId(Integer orderId) {
        return reviewDAO.findByOrderId(orderId);
    }

    public List<Review> getReviewsByServiceId(Integer serviceId) {
        return reviewDAO.findByServiceId(serviceId);
    }

    public List<Review> getReviewsByStaffId(Integer staffId) {
        return reviewDAO.findByStaffId(staffId);
    }

    public List<Review> getReviewsByUserId(Integer userId) {
        return reviewDAO.findByUserId(userId);
    }

    public List<Review> getReviewsByRating(Integer rating) {
        return reviewDAO.findByRating(rating);
    }

    public Double getAverageRatingByServiceId(Integer serviceId) {
        return reviewDAO.getAverageRatingByServiceId(serviceId);
    }

    public Double getAverageRatingByStaffId(Integer staffId) {
        return reviewDAO.getAverageRatingByStaffId(staffId);
    }

    public long getReviewCountByServiceId(Integer serviceId) {
        return reviewDAO.countByServiceId(serviceId);
    }

    public long getReviewCountByStaffId(Integer staffId) {
        return reviewDAO.countByStaffId(staffId);
    }

    public boolean addReview(Review review) {
        if (review == null || review.getOrderId() == null || review.getUserId() == null ||
            review.getServiceId() == null || review.getRating() == null) {
            return false;
        }
        
        
        if (review.getRating() < 1 || review.getRating() > 5) {
            return false;
        }
        
        
        if (isOrderReviewed(review.getOrderId())) {
            return false;
        }
        
        review.setCreateTime(new Date());
        return reviewDAO.insert(review) > 0;
    }

    public boolean updateReview(Review review) {
        if (review == null || review.getReviewId() == null) {
            return false;
        }
        
        
        if (review.getRating() != null && (review.getRating() < 1 || review.getRating() > 5)) {
            return false;
        }
        
        review.setUpdateTime(new Date());
        return reviewDAO.update(review) > 0;
    }

    public boolean deleteReview(Integer reviewId) {
        return reviewDAO.delete(reviewId) > 0;
    }

    public long getReviewCount() {
        return reviewDAO.count();
    }

    public boolean isOrderReviewed(Integer orderId) {
        return reviewDAO.isOrderReviewed(orderId);
    }

    public boolean submitReview(Integer orderId, Integer userId, Integer rating, String content) {
        if (orderId == null || userId == null || rating == null) {
            return false;
        }
        
        
        if (rating < 1 || rating > 5) {
            return false;
        }
        
        
        Order order = orderDAO.findById(orderId);
        if (order == null || order.getStatus() != 4) { 
            return false;
        }
        
        
        if (!order.getUserId().equals(userId)) {
            return false;
        }
        
        
        if (isOrderReviewed(orderId)) {
            return false;
        }
        
        
        Review review = new Review();
        review.setOrderId(orderId);
        review.setUserId(userId);
        review.setServiceId(order.getServiceId());
        review.setStaffId(order.getStaffId());
        review.setRating(rating);
        review.setContent(content);
        review.setCreateTime(new Date());
        
        return reviewDAO.insert(review) > 0;
    }
} 